// var o;
// exports.LevelConfig = void 0;
// var AL_GAME_HUA_GameData = require("./AL_GAME_HUA_GameData");
// var AL_GAME_HUA_ConfigHandler = require("./AL_GAME_HUA_ConfigHandler");
// var AL_GAME_HUA_GameConst = require("./AL_GAME_HUA_GameConst");
// var u = [
//     {
//         ID: 3,
//         Lattce: 18,
//         Type: 120,
//         Number: 360,
//         Empty: 6,
//         Time: 820,
//         Null0: [0.3, 0.3, 0.3],
//         Null1: [0.9, 0.9, 0.9],
//         Null2: [0.2, 0.2, 0.2],
//         Null3: [0.5, 0.5, 0.2],
//         shelf_type: 1,
//         Specialgrid1: 0,
//         Specialgrid2: 0,
//         Storey: 0,
//         latticeType: "1_18",
//         difficult: 1
//     }
// ];
// var p = (function (e) {
//     function t() {
//         var t = e.call(this) || this;
//         t.configs = [];
//         t.challengeConfigs = [];
//         t.specialConfigs = [];
//         var n = AL_GAME_HUA_GameConst.GameConst.getInstance();
//         if (n.lv2hard_enabled) {
//             var o = u[0];
//             var i = n.lv2confs;
//             var r = (i[0], i[1], i[2]);
//             var a = i[3];
//             var s = i[4];
//             var c = i[5];
//             var p = i[6];
//             var f = i[7];
//             var d = i[8];
//             o.Type = r;
//             o.Time = s;
//             o.Null0 = c instanceof Array ? c : [c, c, c];
//             o.Null1 = p instanceof Array ? p : [p, p, p];
//             o.Null2 = f instanceof Array ? f : [f, f, f];
//             o.Null3 = d instanceof Array ? d : [d, d, d];
//             o.Number = a;
//         }
//         return t;
//     }
//     __extends(t, e);
//     t.getInstance = function () {
//         return this.instance || (this.instance = new t());
//     };
//     t.prototype.destroy = function () {
//         t.instance = null;
//     };
//     t.prototype.jiazai_peizhi = function () {
//         return __awaiter(this, void 0, Promise, function () {
//             var e;
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             var c;
//             var u;
//             var p;
//             var f;
//             var d;
//             var h;
//             var _;
//             var P;
//             var g;
//             var y;
//             var m;
//             var v;
//             var D;
//             var L;
//             var b;
//             var C;
//             var H;
//             var I;
//             var S;
//             var w;
//             return __generator(this, function (a) {
//                 switch (a.label) {
//                     case 0:
//                         return [4, this.yibu_jiazai_txt_peizhi("LevelConfig")];
//                     case 1:
//                         if ((e = a.sent())) {
//                             t = e;
//                             this.configs = t;
//                             n = 0;
//                             for (o = this.configs; n < o.length; n++) {
//                                 "number" == typeof (v = o[n]).Null0 && (v.Null0 = [v.Null0, v.Null0, v.Null0]);
//                                 "number" == typeof v.Null1 && (v.Null1 = [v.Null1, v.Null1, v.Null1]);
//                                 "number" == typeof v.Null2 && (v.Null2 = [v.Null2, v.Null2, v.Null2]);
//                                 "number" == typeof v.Null3 && (v.Null3 = [v.Null3, v.Null3, v.Null3]);
//                             }
//                             AL_GAME_HUA_GameData.GameData.getInstance();
//                             i = AL_GAME_HUA_GameConst.GameConst.getInstance().lvconfs;
//                             r = 0;
//                             for (c = i; r < c.length; r++) {
//                                 v = c[r];
//                                 u = v[0];
//                                 v[1];
//                                 p = v[2];
//                                 w = v[3];
//                                 f = v[4];
//                                 d = v[5];
//                                 h = v[6];
//                                 _ = v[7];
//                                 P = v[8];
//                                 (S = t[u - 1]).Type = p;
//                                 S.Time = f;
//                                 S.Null0 = d instanceof Array ? d : [d, d, d];
//                                 S.Null1 = h instanceof Array ? h : [h, h, h];
//                                 S.Null2 = _ instanceof Array ? _ : [_, _, _];
//                                 S.Null3 = P instanceof Array ? P : [P, P, P];
//                                 S.Number = w;
//                             }
//                             g = AL_GAME_HUA_GameConst.GameConst.getInstance().lvspaceConfs;
//                             y = 0;
//                             for (m = g; y < m.length; y++) {
//                                 v = m[y];
//                                 D = v[0];
//                                 L = v[1];
//                                 b = v[2];
//                                 C = D[0];
//                                 H = D[1];
//                                 for (I = C; I <= H; I++) {
//                                     if ((S = t[I - 1])) {
//                                         S.Type = Math.floor(S.Type * L);
//                                         w = Math.floor(S.Number * L);
//                                         S.Number = Math.max(3 * Math.floor(w / 3), 3 * S.Type);
//                                         S.Time = Math.floor(S.Time * b);
//                                     }
//                                 }
//                             }
//                             return [2, t];
//                         }
//                         return [2, null];
//                 }
//             });
//         });
//     };
//     t.prototype.jiazai_tiaozhan_guanqia_peizhi = function () {
//         return __awaiter(this, void 0, Promise, function () {
//             var e;
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             var s;
//             return __generator(this, function (a) {
//                 switch (a.label) {
//                     case 0:
//                         return [4, this.yibu_jiazai_txt_peizhi("LevelConfig_Challenge")];
//                     case 1:
//                         if ((e = a.sent())) {
//                             t = e;
//                             this.challengeConfigs = t;
//                             n = 0;
//                             for (o = this.challengeConfigs; n < o.length; n++) {
//                                 i = o[n];
//                                 r = i[0];
//                                 s = i[1];
//                                 "number" == typeof r.Null0 && (r.Null0 = [r.Null0, r.Null0, r.Null0]);
//                                 "number" == typeof r.Null1 && (r.Null1 = [r.Null1, r.Null1, r.Null1]);
//                                 "number" == typeof r.Null2 && (r.Null2 = [r.Null2, r.Null2, r.Null2]);
//                                 "number" == typeof r.Null3 && (r.Null3 = [r.Null3, r.Null3, r.Null3]);
//                                 "number" == typeof s.Null0 && (s.Null0 = [s.Null0, s.Null0, s.Null0]);
//                                 "number" == typeof s.Null1 && (s.Null1 = [s.Null1, s.Null1, s.Null1]);
//                                 "number" == typeof s.Null2 && (s.Null2 = [s.Null2, s.Null2, s.Null2]);
//                                 "number" == typeof s.Null3 && (s.Null3 = [s.Null3, s.Null3, s.Null3]);
//                             }
//                             return [2, t];
//                         }
//                         return [2, null];
//                 }
//             });
//         });
//     };
//     t.prototype.jiazai_teshu_guanqia_peizhi = function () {
//         return __awaiter(this, void 0, Promise, function () {
//             var e;
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             var s;
//             var c;
//             var u;
//             var p;
//             var f;
//             var d;
//             var h;
//             var _;
//             var P;
//             var g;
//             var y;
//             var m;
//             var v;
//             var D;
//             var L;
//             var b;
//             var C;
//             var H;
//             var I;
//             var S;
//             return __generator(this, function (a) {
//                 switch (a.label) {
//                     case 0:
//                         return [4, this.yibu_jiazai_txt_peizhi("LevelConfig_Special")];
//                     case 1:
//                         if ((e = a.sent())) {
//                             t = e;
//                             this.specialConfigs = t;
//                             n = 0;
//                             for (o = this.specialConfigs; n < o.length; n++) {
//                                 "number" == typeof (m = o[n]).Null0 && (m.Null0 = [m.Null0, m.Null0, m.Null0]);
//                                 "number" == typeof m.Null1 && (m.Null1 = [m.Null1, m.Null1, m.Null1]);
//                                 "number" == typeof m.Null2 && (m.Null2 = [m.Null2, m.Null2, m.Null2]);
//                                 "number" == typeof m.Null3 && (m.Null3 = [m.Null3, m.Null3, m.Null3]);
//                             }
//                             i = AL_GAME_HUA_GameConst.GameConst.getInstance().special_lvconfs;
//                             r = 0;
//                             for (s = i; r < s.length; r++) {
//                                 m = s[r];
//                                 c = m[0];
//                                 m[1];
//                                 u = m[2];
//                                 S = m[3];
//                                 p = m[4];
//                                 f = m[5];
//                                 d = m[6];
//                                 h = m[7];
//                                 _ = m[8];
//                                 (I = t[c - 1]).Type = u;
//                                 I.Time = p;
//                                 I.Null0 = f instanceof Array ? f : [f, f, f];
//                                 I.Null1 = d instanceof Array ? d : [d, d, d];
//                                 I.Null2 = h instanceof Array ? h : [h, h, h];
//                                 I.Null3 = _ instanceof Array ? _ : [_, _, _];
//                                 I.Number = S;
//                             }
//                             P = AL_GAME_HUA_GameConst.GameConst.getInstance().special_spaceConfs;
//                             g = 0;
//                             for (y = P; g < y.length; g++) {
//                                 m = y[g];
//                                 v = m[0];
//                                 D = m[1];
//                                 L = m[2];
//                                 b = v[0];
//                                 C = v[1];
//                                 for (H = b; H <= C; H++) {
//                                     if ((I = t[H - 1])) {
//                                         I.Type = Math.floor(I.Type * D);
//                                         S = Math.floor(I.Number * D);
//                                         I.Number = Math.max(3 * Math.floor(S / 3), 3 * I.Type);
//                                         I.Time = Math.floor(I.Time * L);
//                                     }
//                                 }
//                             }
//                             return [2, t];
//                         }
//                         return [2, null];
//                 }
//             });
//         });
//     };
//     t.prototype.huoqu_peizhi_by_level = function (e) {
//         return e == AL_GAME_HUA_GameConst.GameConst.getInstance().lv2hard_level &&
//             AL_GAME_HUA_GameConst.GameConst.getInstance().lv2hard_enabled
//             ? u[0]
//             : (e >= this.configs.length && (e = 500 + (e % 500)), this.configs[e - 1]);
//     };
//     t.prototype.huoqu_tiaozhan_guanqia_peizhi_by_date = function (e) {
//         var t = (AL_GAME_HUA_GameData.GameData.getInstance().huoqu_tiaozhan_riqi() - 1) % this.challengeConfigs.length;
//         return this.challengeConfigs[t][e - 1];
//     };
//     t.prototype.huoqu_teshu_guanqia_peizhi_by_level = function (e) {
//         return this.specialConfigs[e - 1];
//     };
//     t.prototype.huoqu_teshu_guanqia_zong_shuliang = function () {
//         return this.specialConfigs.length;
//     };
//     t.prototype.huoqu_teshu_guanqia_shifou_jiesuo = function () {
//         return (
//             AL_GAME_HUA_GameData.GameData.getInstance().huoqu_yijiesuo_guanqia() >=
//             AL_GAME_HUA_GameConst.GameConst.getInstance().special_unlock_level
//         );
//     };
//     t.prototype.huoqu_teshu_guanqia_shifou_keyong = function () {
//         return AL_GAME_HUA_GameConst.GameConst.getInstance().special_enabled;
//     };
//     t.prototype.huoqu_tiaozhan_guanqia_shifou_keyong = function () {
//         return AL_GAME_HUA_GameConst.GameConst.getInstance().challenge_enabled;
//     };
//     t.prototype.huoqu_tiaozhan_guanqia_shifou_jiesuo = function () {
//         return (
//             AL_GAME_HUA_GameData.GameData.getInstance().huoqu_yijiesuo_guanqia() >=
//             AL_GAME_HUA_GameConst.GameConst.getInstance().challenge_unlock_level
//         );
//     };
//     t.prototype.huoqu_tiaozhan_guanqia_zidong_pop = function () {
//         return (
//             !!this.huoqu_tiaozhan_guanqia_shifou_keyong() &&
//             !!this.huoqu_tiaozhan_guanqia_shifou_jiesuo() &&
//             AL_GAME_HUA_GameData.GameData.getInstance().huoqu_tiaozhan_pop()
//         );
//     };
//     t.instance = null;
//     return t;
// })(AL_GAME_HUA_ConfigHandler.ConfigHandler);
// exports.LevelConfig = p;


/** Class representing LevelConfig. */
var levelInstance;
exports.LevelConfig = void 0;
const GameData = require("./AL_GAME_HUA_GameData");
const ConfigHandler = require("./AL_GAME_HUA_ConfigHandler");
const GameConst = require("./AL_GAME_HUA_GameConst");

const defaultConfig = [
    {
        ID: 3,
        Lattce: 18,
        Type: 120,
        Number: 360,
        Empty: 6,
        Time: 820,
        Null0: [0.3, 0.3, 0.3],
        Null1: [0.9, 0.9, 0.9],
        Null2: [0.2, 0.2, 0.2],
        Null3: [0.5, 0.5, 0.2],
        shelf_type: 1,
        Specialgrid1: 0,
        Specialgrid2: 0,
        Storey: 0,
        latticeType: "1_18",
        difficult: 1
    }
];

/**
 * Main class for managing game configurations.
 */
class LevelConfig extends ConfigHandler.ConfigHandler {

    constructor() {
        super();
        this.configs = [];
        this.challengeConfigs = [];
        this.specialConfigs = [];

        const gameConstInstance = GameConst.GameConst.getInstance();
        if (gameConstInstance.lv2hard_enabled) {
            const initialConfig = defaultConfig[0];
            const configValues = gameConstInstance.lv2confs;

            initialConfig.Type = configValues[2];
            initialConfig.Time = configValues[4];
            initialConfig.Null0 = Array.isArray(configValues[5]) ? configValues[5] : Array(3).fill(configValues[5]);
            initialConfig.Null1 = Array.isArray(configValues[6]) ? configValues[6] : Array(3).fill(configValues[6]);
            initialConfig.Null2 = Array.isArray(configValues[7]) ? configValues[7] : Array(3).fill(configValues[7]);
            initialConfig.Null3 = Array.isArray(configValues[8]) ? configValues[8] : Array(3).fill(configValues[8]);
            initialConfig.Number = configValues[3];
        }
    }

    /** 
     * Get singleton instance of LevelConfig
     * @returns {LevelConfig} Singleton instance
     */
    static getInstance() {
        return this.instance || (this.instance = new LevelConfig());
    }

    /** Destroys existing instance */
    destroy() {
        LevelConfig.instance = null;
    }

    /**
     * Loads configurations from a text file.
     * @returns {Promise<any>} Promise resolving to configuration data
     */
    async jiazai_peizhi() {
        const fileContent = await this.yibu_jiazai_txt_peizhi("LevelConfig");
        if (fileContent) {
            this.configs = fileContent;
            this.configs.forEach((config) => {
                if (typeof config.Null0 === 'number') config.Null0 = [config.Null0, config.Null0, config.Null0];
                if (typeof config.Null1 === 'number') config.Null1 = [config.Null1, config.Null1, config.Null1];
                if (typeof config.Null2 === 'number') config.Null2 = [config.Null2, config.Null2, config.Null2];
                if (typeof config.Null3 === 'number') config.Null3 = [config.Null3, config.Null3, config.Null3];
            });

            const gameDataInstance = GameData.GameData.getInstance();
            const gameConstInstance = GameConst.GameConst.getInstance();
            
            const levelConfigs = gameConstInstance.lvconfs;
            
            levelConfigs.forEach((levelConfig) => {
                const configIndex = levelConfig[0] - 1;
                const levelConfigValues = this.configs[configIndex];

                levelConfigValues.Type = levelConfig[2];
                levelConfigValues.Time = levelConfig[4];
                levelConfigValues.Null0 = Array.isArray(levelConfig[5]) ? levelConfig[5] : Array(3).fill(levelConfig[5]);
                levelConfigValues.Null1 = Array.isArray(levelConfig[6]) ? levelConfig[6] : Array(3).fill(levelConfig[6]);
                levelConfigValues.Null2 = Array.isArray(levelConfig[7]) ? levelConfig[7] : Array(3).fill(levelConfig[7]);
                levelConfigValues.Null3 = Array.isArray(levelConfig[8]) ? levelConfig[8] : Array(3).fill(levelConfig[8]);
                levelConfigValues.Number = levelConfig[3];
            });

            const levelSpaceConfigs = gameConstInstance.lvspaceConfs;
            levelSpaceConfigs.forEach((spaceConfig) => {
                const [range, multiplier, timeMultiplier] = spaceConfig;
                for (let i = range[0]; i <= range[1]; i++) {
                    const config = this.configs[i - 1];
                    if (config) {
                        config.Type = Math.floor(config.Type * multiplier);
                        const calculatedNumber = Math.floor(config.Number * multiplier);
                        config.Number = Math.max(3 * Math.floor(calculatedNumber / 3), 3 * config.Type);
                        config.Time = Math.floor(config.Time * timeMultiplier);
                    }
                }
            });

            return this.configs;
        }
        return null;
    }

    /**
     * Loads challenge configurations.
     * @returns {Promise<any>} Promise resolving to challenge configuration data
     */
    async jiazai_tiaozhan_guanqia_peizhi() {
        const fileContent = await this.yibu_jiazai_txt_peizhi("LevelConfig_Challenge");
        if (fileContent) {
            this.challengeConfigs = fileContent;
            this.challengeConfigs.forEach(([config, subConfig]) => {
                if (typeof config.Null0 === 'number') config.Null0 = [config.Null0, config.Null0, config.Null0];
                if (typeof config.Null1 === 'number') config.Null1 = [config.Null1, config.Null1, config.Null1];
                if (typeof config.Null2 === 'number') config.Null2 = [config.Null2, config.Null2, config.Null2];
                if (typeof config.Null3 === 'number') config.Null3 = [config.Null3, config.Null3, config.Null3];
                if (typeof subConfig.Null0 === 'number') subConfig.Null0 = [subConfig.Null0, subConfig.Null0, subConfig.Null0];
                if (typeof subConfig.Null1 === 'number') subConfig.Null1 = [subConfig.Null1, subConfig.Null1, subConfig.Null1];
                if (typeof subConfig.Null2 === 'number') subConfig.Null2 = [subConfig.Null2, subConfig.Null2, subConfig.Null2];
                if (typeof subConfig.Null3 === 'number') subConfig.Null3 = [subConfig.Null3, subConfig.Null3, subConfig.Null3];
            });
            return this.challengeConfigs;
        }
        return null;
    }

    /**
     * Loads special configurations.
     * @returns {Promise<any>} Promise resolving to special configuration data
     */
    async jiazai_teshu_guanqia_peizhi() {
        const fileContent = await this.yibu_jiazai_txt_peizhi("LevelConfig_Special");
        if (fileContent) {
            this.specialConfigs = fileContent;
            this.specialConfigs.forEach((config) => {
                if (typeof config.Null0 === 'number') config.Null0 = [config.Null0, config.Null0, config.Null0];
                if (typeof config.Null1 === 'number') config.Null1 = [config.Null1, config.Null1, config.Null1];
                if (typeof config.Null2 === 'number') config.Null2 = [config.Null2, config.Null2, config.Null2];
                if (typeof config.Null3 === 'number') config.Null3 = [config.Null3, config.Null3, config.Null3];
            });

            const specialConfigs = GameConst.GameConst.getInstance().special_lvconfs;
            specialConfigs.forEach((specialConfig) => {
                const levelIndex = specialConfig[0] - 1;
                const levelConfigValues = this.specialConfigs[levelIndex];
                
                levelConfigValues.Type = specialConfig[2];
                levelConfigValues.Time = specialConfig[4];
                levelConfigValues.Null0 = Array.isArray(specialConfig[5]) ? specialConfig[5] : Array(3).fill(specialConfig[5]);
                levelConfigValues.Null1 = Array.isArray(specialConfig[6]) ? specialConfig[6] : Array(3).fill(specialConfig[6]);
                levelConfigValues.Null2 = Array.isArray(specialConfig[7]) ? specialConfig[7] : Array(3).fill(specialConfig[7]);
                levelConfigValues.Null3 = Array.isArray(specialConfig[8]) ? specialConfig[8] : Array(3).fill(specialConfig[8]);
                levelConfigValues.Number = specialConfig[3];
            });

            const specialSpaceConfigs = GameConst.GameConst.getInstance().special_spaceConfs;
            specialSpaceConfigs.forEach((spaceConfig) => {
                const [range, multiplier, timeMultiplier] = spaceConfig;
                for (let i = range[0]; i <= range[1]; i++) {
                    const config = this.specialConfigs[i - 1];
                    if (config) {
                        config.Type = Math.floor(config.Type * multiplier);
                        const calculatedNumber = Math.floor(config.Number * multiplier);
                        config.Number = Math.max(3 * Math.floor(calculatedNumber / 3), 3 * config.Type);
                        config.Time = Math.floor(config.Time * timeMultiplier);
                    }
                }
            });

            return this.specialConfigs;
        }
        return null;
    }

    /**
     * Returns the configuration for a given level.
     * @param {number} level - Level number
     * @returns {Object} Level configuration
     */
    huoqu_peizhi_by_level(level) {
        const gameConstInstance = GameConst.GameConst.getInstance();

        if (level === gameConstInstance.lv2hard_level && gameConstInstance.lv2hard_enabled) {
            return defaultConfig[0];
        }

        if (level >= this.configs.length) {
            level = 500 + (level % 500);
        }

        return this.configs[level - 1];
    }

    /**
     * Returns challenge configuration based on date.
     * @param {number} dayIndex - Day index
     * @returns {Object} Challenge configuration data
     */
    huoqu_tiaozhan_guanqia_peizhi_by_date(dayIndex) {
        const challengeIndex = (GameData.GameData.getInstance().huoqu_tiaozhan_riqi() - 1) % this.challengeConfigs.length;
        return this.challengeConfigs[challengeIndex][dayIndex - 1];
    }

    /**
     * Get special configuration for a given level.
     * @param {number} level - Special level number
     * @returns {Object} Special configuration
     */
    huoqu_teshu_guanqia_peizhi_by_level(level) {
        return this.specialConfigs[level - 1];
    }

    /**
     * Get total number of special levels.
     * @returns {number} Number of special levels
     */
    huoqu_teshu_guanqia_zong_shuliang() {
        return this.specialConfigs.length;
    }

    /**
     * Check if special is unlocked.
     * @returns {boolean} True if special is unlocked, false otherwise
     */
    huoqu_teshu_guanqia_shifou_jiesuo() {
        return (
            GameData.GameData.getInstance().huoqu_yijiesuo_guanqia() >=
            GameConst.GameConst.getInstance().special_unlock_level
        );
    }

    /**
     * Check if special is enabled.
     * @returns {boolean} True if special is enabled, false otherwise
     */
    huoqu_teshu_guanqia_shifou_keyong() {
        return GameConst.GameConst.getInstance().special_enabled;
    }

    /**
     * Check if challenge is enabled.
     * @returns {boolean} True if challenge is enabled, false otherwise
     */
    huoqu_tiaozhan_guanqia_shifou_keyong() {
        return GameConst.GameConst.getInstance().challenge_enabled;
    }

    /**
     * Check if challenge is unlocked.
     * @returns {boolean} True if challenge is unlocked, false otherwise
     */
    huoqu_tiaozhan_guanqia_shifou_jiesuo() {
        return (
            GameData.GameData.getInstance().huoqu_yijiesuo_guanqia() >=
            GameConst.GameConst.getInstance().challenge_unlock_level
        );
    }

    /**
     * Check if challenge can auto-pop.
     * @returns {boolean} True if challenge can auto-pop, false otherwise
     */
    huoqu_tiaozhan_guanqia_zidong_pop() {
        return (
            this.huoqu_tiaozhan_guanqia_shifou_keyong() &&
            this.huoqu_tiaozhan_guanqia_shifou_jiesuo() &&
            GameData.GameData.getInstance().huoqu_tiaozhan_pop()
        );
    }
}

LevelConfig.instance = null;
exports.LevelConfig = LevelConfig;